﻿namespace SugarTank.Mobile.Managers
{
    using System;
    using CommonTypes.Phone;
    using Contracts;
    using Helpers;
    using Microsoft.Phone.Tasks;

    public class ChooserManager : IChooserManager
    {
        #region IChooserManager Members

        /// <summary>
        /// Occurs when [save phone number complete].
        /// </summary>
        public event EventHandler<EventArgs<TaskResult>> SavePhoneNumberTaskCompleted;

        /// <summary>
        /// Occurs when [save phone number task failed].
        /// </summary>
        public event EventHandler<EventArgs<Exception>> SavePhoneNumberTaskFailed;

        /// <summary>
        /// Occurs when [choose photo task completed].
        /// </summary>
        public event EventHandler<EventArgs<PhotoResult>> ChoosePhotoTaskCompleted;

        /// <summary>
        /// Occurs when [choose photo task failed].
        /// </summary>
        public event EventHandler<EventArgs<Exception>> ChoosePhotoTaskFailed;

        /// <summary>
        /// Occurs when [save email address task completed].
        /// </summary>
        public event EventHandler<EventArgs<TaskResult>> SaveEmailAddressTaskCompleted;

        /// <summary>
        /// Occurs when [save email address task failed].
        /// </summary>
        public event EventHandler<EventArgs<Exception>> SaveEmailAddressTaskFailed;

        /// <summary>
        /// Occurs when [choose phone number completed].
        /// </summary>
        public event EventHandler<EventArgs<PhoneNumberResult>> ChoosePhoneNumberCompleted;

        /// <summary>
        /// Occurs when [choose phone number failed].
        /// </summary>
        public event EventHandler<EventArgs<Exception>> ChoosePhoneNumberFailed;

        /// <summary>
        /// Occurs when [choose email completed].
        /// </summary>
        public event EventHandler<EventArgs<EmailResult>> ChooseEmailCompleted;

        /// <summary>
        /// Occurs when [choose email failed].
        /// </summary>
        public event EventHandler<EventArgs<Exception>> ChooseEmailFailed;

        /// <summary>
        /// Occurs when [take picture completed].
        /// </summary>
        public event EventHandler<EventArgs<PhotoResult>> TakePhotoCompleted;

        /// <summary>
        /// Occurs when [take picture failed].
        /// </summary>
        public event EventHandler<EventArgs<Exception>> TakePhotoFailed;

        /// <summary>
        /// Saves the phone number.
        /// </summary>
        /// <param name="phoneNumber">The phone number.</param>
        public void SavePhoneNumber(PhoneNumber phoneNumber)
        {
            var savePhoneNumberTask = new SavePhoneNumberTask();
            savePhoneNumberTask.Completed += OnSavePhoneNumberTaskCompleted;
            savePhoneNumberTask.PhoneNumber = phoneNumber.Number;
            savePhoneNumberTask.Show();
        }

        /// <summary>
        /// Chooses a photo.
        /// </summary>
        public void ChoosePhoto()
        {
            var photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += OnChoosePhotoTaskCompleted;
            photoChooserTask.Show();
        }

        /// <summary>
        /// Saves the email address.
        /// </summary>
        /// <param name="emailAddress">The email address.</param>
        public void SaveEmailAddress(string emailAddress)
        {
            var saveEmailAddressTask = new SaveEmailAddressTask();
            saveEmailAddressTask.Completed += OnSaveEmailTaskCompleted;
            saveEmailAddressTask.Email = emailAddress;
            saveEmailAddressTask.Show();
        }

        /// <summary>
        /// Chooses the phone number.
        /// </summary>
        public void ChoosePhoneNumber()
        {
            var phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += OnPhoneNumberChooserTaskCompleted;
            phoneNumberChooserTask.Show();
        }

        /// <summary>
        /// Chooses an email address.
        /// </summary>
        public void ChooseEmail()
        {
            var emailAddressChooserTask = new EmailAddressChooserTask();
            emailAddressChooserTask.Completed += OnEmailAddressChooserTaskCompleted;
            emailAddressChooserTask.Show();
        }

        /// <summary>
        /// Takes a photo.
        /// </summary>
        public void TakePhoto()
        {
            var cameraCaptureTask = new CameraCaptureTask();
            cameraCaptureTask.Completed += OnCameraCaptureTaskTaskCompleted;
            cameraCaptureTask.Show();
        }

        #endregion

        /// <summary>
        /// Called when [camera capture task task completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void OnCameraCaptureTaskTaskCompleted(object sender, PhotoResult e)
        {
            if (e.Error == null)
            {
                TakePhotoCompleted.FireEvent(this, () => new EventArgs<PhotoResult>(e));
            }
            else
            {
                TakePhotoFailed.FireEvent(this, () => new EventArgs<Exception>(e.Error));
            }
        }

        /// <summary>
        /// Called when [email address chooser task completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void OnEmailAddressChooserTaskCompleted(object sender, EmailResult e)
        {
            if (e.Error == null)
            {
                ChooseEmailCompleted.FireEvent(this, () => new EventArgs<EmailResult>(e));
            }
            else
            {
                ChooseEmailFailed.FireEvent(this, () => new EventArgs<Exception>(e.Error));
            }
        }

        /// <summary>
        /// Called when [phone number chooser task completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void OnPhoneNumberChooserTaskCompleted(object sender, PhoneNumberResult e)
        {
            if (e.Error == null)
            {
                ChoosePhoneNumberCompleted.FireEvent(this, () => new EventArgs<PhoneNumberResult>(e));
            }
            else
            {
                ChoosePhoneNumberFailed.FireEvent(this, () => new EventArgs<Exception>(e.Error));
            }
        }

        /// <summary>
        /// Called when [save email task completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Phone.Tasks.TaskEventArgs"/> instance containing the event data.</param>
        private void OnSaveEmailTaskCompleted(object sender, TaskEventArgs e)
        {
            if (e.Error == null)
            {
                SaveEmailAddressTaskCompleted.FireEvent(this, () => new EventArgs<TaskResult>(e.TaskResult));
            }
            else
            {
                SaveEmailAddressTaskFailed.FireEvent(this, () => new EventArgs<Exception>(e.Error));
            }
        }

        /// <summary>
        /// Called when [choose photo completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void OnChoosePhotoTaskCompleted(object sender, PhotoResult e)
        {
            if (e.Error == null)
            {
                ChoosePhotoTaskCompleted.FireEvent(this, () => new EventArgs<PhotoResult>(e));
            }
            else
            {
                ChoosePhotoTaskFailed.FireEvent(this, () => new EventArgs<Exception>(e.Error));
            }
        }

        /// <summary>
        /// Saves the phone number task completed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Phone.Tasks.TaskEventArgs"/> instance containing the event data.</param>
        private void OnSavePhoneNumberTaskCompleted(object sender, TaskEventArgs e)
        {
            if (e.Error == null)
            {
                SavePhoneNumberTaskCompleted.FireEvent(this, () => new EventArgs<TaskResult>(e.TaskResult));
            }
            else
            {
                SavePhoneNumberTaskFailed.FireEvent(this, () => new EventArgs<Exception>(e.Error));
            }
        }
    }
}